package Clases;

import java.io.File;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class SimpleSearcher {

	public static void main(String[] args) throws Exception {

		File indice = new File ("C:/Users/maureyes/workspace/wikipedia/indice");
		String consulta = "hola";
		StandardAnalyzer analizador = new StandardAnalyzer(Version.LUCENE_30);
		Query consultaEnFormatoLucene = new QueryParser(Version.LUCENE_30, "contenido", analizador).parse(consulta);

		Directory directorio = FSDirectory.open(indice);
		IndexSearcher buscador = new IndexSearcher(directorio);

		TopScoreDocCollector documentos = TopScoreDocCollector.create(4023,true);
		buscador.search(consultaEnFormatoLucene, documentos);
		ScoreDoc[] hits = documentos.topDocs().scoreDocs;

		for(ScoreDoc docu : hits) {
			Document d = buscador.doc(docu.doc);
			System.out.println(d.get("filename"));
		}

	}
	//TopDoc collector = TopScoreDocCollector.create(hitCantMaxPorPag, true);

	/*
		File indexDir = new File("C:/Users/ana/Desktop/ObliIngInf/pruebaIndice");
		String query = "remolacha";
		int hits = 4032; //numero maximo de hits

		SimpleSearcher searcher = new SimpleSearcher();
		searcher.searchIndex(indexDir, query, hits);

	}

	private void searchIndex(File indexDir, String queryStr, int maxHits)
	throws Exception {  

		Directory directory = FSDirectory.open(indexDir);

		IndexSearcher searcher = new IndexSearcher(directory);
		QueryParser parser = new QueryParser(Version.LUCENE_30,
				"contents", new SimpleAnalyzer());
		Query query = parser.parse(queryStr);

		TopDocs topDocs = searcher.search(query, maxHits);

		ScoreDoc[] hits = topDocs.scoreDocs;
		for (int i = 0; i < hits.length; i++) {
			int docId = hits[i].doc;
			Document d = searcher.doc(docId);
			System.out.println(d.get("filename"));
		}

		System.out.println("Found " + hits.length);
	 */
	//	}

}
